<?php

namespace app\model;

use think\Model;

class PigHealth extends Model 
{
    const IS_DELETE_NO = 0;

    /**
     * @Description 根据 out_id 判断数据是否存在
     * @author: zj
     * @param int $factory_id
     * @param int $out_id
     * @time 2024/03/27 10:16:08
     * @return false|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkOutIdIsExist(int $factory_id,int $out_id)
    {
        $info = $this->field('id')
            ->where('factory_id',$factory_id)
            ->where('out_id',$out_id)
            ->where('delete_time',self::IS_DELETE_NO)
            ->find();
        if (!$info) {
            return false;
        }

        return $info['id'];
    }

    /**
     * @Description 获取生猪的最新健康数据
     * @author: zj
     * @param int $factory_id
     * @param int $pig_id
     * @time 2024/03/27 10:20:15
     * @return null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function lastHealthData(int $factory_id,int $pig_id)
    {
        $info = $this->field('out_id as id,temperature,heart_rate,sao2,pi,breathe')
            ->where('factory_id',$factory_id)
            ->where('pig_id',$pig_id)
            ->order('out_id DESC')
            ->find();
        if (!$info) {
            return null;
        }

        return $info->toArray();
    }

}